var gsjmap = {};
gsjmap.bounds  = [113.747505187988,22.3982124328613,114.626457214355,22.8613185882568];
gsjmap.mapUrl = "http://192.168.0.6:9898";
var baseLayer = new ol.layer.Tile({//地图背景图层定义
	opacity:1,
	layerName:"gsjBase",
	source:new ol.source.TileWMS({
		url:gsjmap.mapUrl+"/geoserver/GSJ/wms",
		params:{
			'LAYERS':"beijing",
			'TTLED':true
		},
		crossOrigin:"anonymous",
		serverType:'geoserver'  
	})
});

var gdjhzd = new ol.layer.Tile({
	opacity:1,
	layerName:"gd",
	source:new ol.source.TileWMS({
		url:gsjmap.mapUrl+"/geoserver/GSJ/wms",
		params:{
			'LAYERS':"gdjhzd",
			'TTLED':true
		},
		crossOrigin:"anonymous",
		serverType:'geoserver'  
	})
});
var sdjhzd = new ol.layer.Tile({
	opacity:1,
	layerName:"shend",
	source:new ol.source.TileWMS({
		url:gsjmap.mapUrl+"/geoserver/GSJ/wms",
		params:{
			'LAYERS':"sdjhzd",
			'TTLED':true
		},
		crossOrigin:"anonymous",
		serverType:'geoserver'  
	})
});
var gsjhzd = new ol.layer.Tile({
	opacity:1,
	layerName:"gs",
	source:new ol.source.TileWMS({
		url:gsjmap.mapUrl+"/geoserver/GSJ/wms",
		params:{
			'LAYERS':"gsjhzds",
			'TTLED':true
		},
		crossOrigin:"anonymous",
		serverType:'geoserver'  
	})
});
var view = new ol.View({//地图视图定义
	extent:gsjmap.bounds,
	center:[114.15894,22.62694],
	zoom:10.8,
	projection:"EPSG:4326",
	minZoom: 10,
	maxZoom: 20
});
var controls = [];//地图控件集合
//鼠标位置坐标
var mousePositionControl = new ol.control.MousePosition({  
	className: 'custom-mouse-position',  
	target: document.getElementById('location'),  
	coordinateFormat: ol.coordinate.createStringXY(5),//保留5位小数  
	undefinedHTML: ' '  
}); 
//比例尺控件
var scaleLineControl = new ol.control.ScaleLine({
	units: "metric", //设置比例尺单位，degrees、imperial、us、nautical、metric（度量单位）
	target:"scaleLine"

});

//实例化地图
var map = new ol.Map({
	view:view,
	target:"map",
	controls:controls,
	layers:[baseLayer],
	logo:false 
});
gsjmap.map = map;
map.on('singleclick',mapClick);

//拉框缩小地图
var dragZoomIn = new ol.interaction.DragZoom({
	out:true,
	condition:ol.events.condition.always
});
dragZoomIn.setActive(false);
gsjmap.map.addInteraction(dragZoomIn);
//拉框放大地图
var dragZoomOut = new ol.interaction.DragZoom({
	out:false,
	condition:ol.events.condition.always
});
dragZoomOut.setActive(false);
gsjmap.map.addInteraction(dragZoomOut);

//地图缩小事件
document.querySelector("#zoom_out").addEventListener('click', function() {
	dragZoomOut.setActive(false);
	dragZoomIn.setActive(true);
	document.querySelector("#map").style.cursor = "crosshair";

}, false);

//地图放大事件
document.querySelector("#zoom_in").addEventListener('click', function() {
	dragZoomIn.setActive(false);
	dragZoomOut.setActive(true);
	document.querySelector("#map").style.cursor = "crosshair";

}, false);

//地图平移
document.querySelector("#translation").addEventListener('click',function(){
	dragZoomIn.setActive(false);
	dragZoomOut.setActive(false);
	document.querySelector("#map").style.cursor = "default";
},false);
//初始化地图
function  initMap(){
	map.getView().setCenter([114.15894,22.62694]);
	map.getView().setZoom(10.8);
	map.getView().setMaxZoom(20);
	map.getView().setMinZoom(10);
	dragZoomIn.setActive(false);
	dragZoomOut.setActive(false);
	document.querySelector("#map").style.cursor = "default";
//	tfxtLayer.setVisible(false);
//	jkxtLayer.setVisible(false);
//	zmxtLayer.setVisible(false);
}
//地图全图
$("#fullExtent").on("click",initMap);

//地图查询符号
var wfsimage = new ol.style.Icon({
	src: '../../gsj/images/gis/mark_red.gif',
	scale: 0.5
});
var wfsstyle = {
		'Point': new ol.style.Style({
			image: wfsimage
		}),
		'MultiPoint': new ol.style.Style({
			image: wfsimage
		}),
		'LineString': new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#00FFFF',
				width: 3
			})
		}),
		'MultiLineString': new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#00FFFF',
				width: 8
			})
		}),
		'Polygon': new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#00FFFF',
				lineDash: [4],
				width: 3
			}),
			fill: new ol.style.Fill({
				color: 'rgba(0, 0, 255, 0.1)'
			})
		}),
		'MultiPolygon': new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#00FFFF',
				lineDash: [4],
				width: 3
			}),
			fill: new ol.style.Fill({
				color: 'rgba(0, 0, 255, 0.1)'
			})
		})
};
var  styleFunction = function(feature){
	return wfsstyle[feature.getGeometry().getType()];
};
//加载专题图
var   layerControl = function(dataLayer){
	if(dataLayer.layerName == 'suidao'){
		var layersArr = [mainMapMsg.sdLayer,
		                 mainMapMsg.hdmLayer,
		                 //  mainMapMsg.sdxLayer
		                 ];
		for(var i = 0;i<layersArr.length;i++){
			addLayers(layersArr[i]);
		}
//		map.addLayer(tfxtLayer);
//		map.addLayer(jkxtLayer);
//		map.addLayer(zmxtLayer);
	}else if(dataLayer.layerName == 'shend'){
		addLayers(dataLayer);
		map.addLayer(sdjhzd);

	}else if(dataLayer.layerName == 'gd'){
		addLayers(dataLayer);
		map.addLayer(gdjhzd);

	}else if(dataLayer.layerName == 'gs'){
		addLayers(dataLayer);
		map.addLayer(gsjhzd);

	}else{
		addLayers(dataLayer);
	}
};
//map.getView().on('change:resolution', function(){
//if(this.getResolution()<=0.000002682209014892578){
//tfxtLayer.setVisible(true);
//jkxtLayer.setVisible(true);
//zmxtLayer.setVisible(true);
//}else if(this.getResolution()>0.000002682209014892578&&tfxtLayer.getVisible()==true){
//tfxtLayer.setVisible(false);
//jkxtLayer.setVisible(false);
//zmxtLayer.setVisible(false);
//}

//});
//地图实例化加载图层
var addLayers = function(dataLayer){
	if(dataLayer!=null){
		var vetorSource = new ol.source.Vector(
				{
					url:function(extent,resolution,projection){
						return dataLayer.layerUrl+extent.join(',');
					},
					format:new ol.format.GeoJSON(),
					strategy:ol.loadingstrategy.bbox 
				});
		var   vectorLayer = new ol.layer.Vector({
			layerName:dataLayer.layerName,
			source:vetorSource,
			style:dataLayer.style,
		});
		map.addLayer(vectorLayer);
	}
};
//地图加载路检专题图层
var addRoadInspect = function(year,roadType){ 
	removeAllLayer();
	if(year == 2011){
		for(var i = 0;i<roadType.length;i++){
			var roadInspect = roadType[i]+2011;
			addLayers(getRoadInspect(roadInspect));
		}
	}else if(year == 2013){
		for(var i = 0;i<roadType.length;i++){
			var roadInspect = roadType[i]+2013;
			addLayers(getRoadInspect(roadInspect));
		}
	}else if(year == 2015){
		for(var i = 0;i<roadType.length;i++){
			var roadInspect = roadType[i]+2015;
			addLayers(getRoadInspect(roadInspect));
		}
	}
};
//根据路检参数获取路检图层的对象
var getRoadInspect = function(name){
	var layer = {};
	switch (name){
	case 'GdInspect2011':
		layer = mainMapMsg.GdInspect2011;
		break;
	case 'GdInspect2013':
		layer = mainMapMsg.GdInspect2013;
		break;
	case 'GdInspect2015':
		layer = mainMapMsg.GdInspect2015;
		break;
	case 'ShengdInspect2011':
		layer = mainMapMsg.ShengdInspect2011;
		break;
	case 'ShengdInspect2013':
		layer = mainMapMsg.ShengdInspect2013;
		break;
	case 'ShengdInspect2015':
		layer = mainMapMsg.ShengdInspect2015;
		break;
	case 'XdInspect2011':
		layer = mainMapMsg.XdInspect2011;
		break;
	case 'XdInspect2015':
		layer = mainMapMsg.XdInspect2015;
		break;
	case 'XiangdInspect2011':
		layer = mainMapMsg.XiangdInspect2011;
		break;
	case 'XiangdInspect2015':
		layer = mainMapMsg.XiangdInspect2015;
		break;
	}
	return layer;
};
//国检柱状图与前端的交互接口
var setRoadEcharts = function(opt){
	//year2,roadtype2,evaluation,sxx
	//year,roadType,quota,sxx
	var tempN = 0;
	for(var temp in opt){
		if(opt[temp].length!=0)
			tempN++;
	}
	removeAllLayer();
	if(tempN==4){
		for(var i =0;i<opt.roadtype2.length;i++){
			addLayers(getQuotaInspect(opt.roadtype2[i]+opt.year2[0]+opt.evaluation+opt.sxx[0]+'Layer'));
		}

	}

};
//根据参数设置调用的柱状图层对象
var getQuotaInspect = function(name){
	var layer = {};
	switch (name){
	case 'gd11MQIUpLayer':
		layer = mainMapMsg.gd11MQIUpLayer;
		break;
	case 'gd11MQIDownLayer':
		layer = mainMapMsg.gd11MQIDownLayer;
		break;
	case 'gd11PQIUpLayer':
		layer = mainMapMsg.gd11PQIUpLayer;
		break;
	case 'gd11PQIDownLayer':
		layer = mainMapMsg.gd11PQIDownLayer;
		break;
	case 'gd11PCIUpLayer':
		layer = mainMapMsg.gd11PCIUpLayer;
		break;
	case 'gd11PCIDownLayer':
		layer = mainMapMsg.gd11PCIDownLayer;
		break;
	case 'gd11RQIUpLayer':
		layer = mainMapMsg.gd11RQIUpLayer;
		break;
	case 'gd11RQIDownLayer':
		layer = mainMapMsg.gd11RQIDownLayer;
		break;
	case 'gd11SCIUpLayer':
		layer = mainMapMsg.gd11SCIUpLayer;
		break;
	case 'gd11SCIDownLayer':
		layer = mainMapMsg.gd11SCIDownLayer;
		break;
	case 'gd11TCIUpLayer':
		layer = mainMapMsg.gd11TCIUpLayer;
		break;
	case 'gd11TCIDownLayer':
		layer = mainMapMsg.gd11TCIDownLayer;
		break;
	case 'gd13MQIUpLayer':
		layer = mainMapMsg.gd13MQIUpLayer;
		break;
	case 'gd13MQIDownLayer':
		layer = mainMapMsg.gd13MQIDownLayer;
		break;
	case 'gd13PQIUpLayer':
		layer = mainMapMsg.gd13MQIUpLayer;
		break;
	case 'gd13PQIDownLayer':
		layer = mainMapMsg.gd13PQIDownLayer;
		break;
	case 'gd13PCIUpLayer':
		layer = mainMapMsg.gd13PCIUpLayer;
		break;
	case 'gd13PCIDownLayer':
		layer = mainMapMsg.gd13PCIDownLayer;
		break;
	case 'gd13RQIUpLayer':
		layer = mainMapMsg.gd13RQIUpLayer;
		break;
	case 'gd13RQIDownLayer':
		layer = mainMapMsg.gd13RQIDownLayer;
		break;
	case 'gd13SCIUpLayer':
		layer = mainMapMsg.gd13SCIUpLayer;
		break;
	case 'gd13SCIDownLayer':
		layer = mainMapMsg.gd13SCIDownLayer;
		break;
	case 'gd13TCIUpLayer':
		layer = mainMapMsg.gd13TCIUpLayer;
		break;
	case 'gd13TCIDownLayer':
		layer = mainMapMsg.gd13TCIDownLayer;
		break;
	case 'gd15MQIUpLayer':
		layer = mainMapMsg.gd15MQIUpLayer;
		break;
	case 'gd15MQIDownLayer':
		layer = mainMapMsg.gd15MQIDownLayer;
		break;
	case 'gd15PQIUpLayer':
		layer = mainMapMsg.gd15PQIUpLayer;
		break;
	case 'gd15PQIDownLayer':
		layer = mainMapMsg.gd15PQIDownLayer;
		break;
	case 'gd15PCIUpLayer':
		layer = mainMapMsg.gd15PCIUpLayer;
		break;
	case 'gd15PCIDownLayer':
		layer = mainMapMsg.gd15PCIDownLayer;
		break;
	case 'gd15RQIUpLayer':
		layer = mainMapMsg.gd15RQIUpLayer;
		break;
	case 'gd15RQIDownLayer':
		layer = mainMapMsg.gd15RQIDownLayer;
		break;
	case 'gd15SCIUpLayer':
		layer = mainMapMsg.gd15SCIUpLayer;
		break;
	case 'gd15SCIDownLayer':
		layer = mainMapMsg.gd15SCIDownLayer;
		break;
	case 'gd15TCIUpLayer':
		layer = mainMapMsg.gd15TCIUpLayer;
		break;
	case 'gd15TCIDownLayer':
		layer = mainMapMsg.gd15TCIDownLayer;
		break;
	case 'sd11MQIUpLayer':
		layer = mainMapMsg.sd11MQIUpLayer;
		break;
	case 'sd11MQIDownLayer':
		layer = mainMapMsg.sd11MQIDownLayer;
		break;
	case 'sd11PQIUpLayer':
		layer = mainMapMsg.sd11PQIUpLayer;
		break;
	case 'sd11PQIDownLayer':
		layer = mainMapMsg.sd11PQIDownLayer;
		break;
	case 'sd11PCIUpLayer':
		layer = mainMapMsg.sd11PCIUpLayer;
		break;
	case 'sd11PCIDownLayer':
		layer = mainMapMsg.sd11PCIDownLayer;
		break;
	case 'sd11RQIUpLayer':
		layer = mainMapMsg.sd11RQIUpLayer;
		break;
	case 'sd11RQIDownLayer':
		layer = mainMapMsg.sd11RQIDownLayer;
		break;
	case 'sd11SCIUpLayer':
		layer = mainMapMsg.sd11SCIUpLayer;
		break;
	case 'sd11SCIDownLayer':
		layer = mainMapMsg.sd11SCIDownLayer;
		break;
	case 'sd11TCIUpLayer':
		layer = mainMapMsg.sd11TCIUpLayer;
		break;
	case 'sd11TCIDownLayer':
		layer = mainMapMsg.sd11TCIDownLayer;
		break;
	case 'sd13MQIUpLayer':
		layer = mainMapMsg.sd13MQIUpLayer;
		break;
	case 'sd13MQIDownLayer':
		layer = mainMapMsg.sd13MQIDownLayer;
		break;
	case 'sd13PQIUpLayer':
		layer = mainMapMsg.sd13PQIUpLayer;
		break;
	case 'sd13PQIDownLayer':
		layer = mainMapMsg.sd13PQIDownLayer;
		break;
	case 'sd13PCIUpLayer':
		layer = mainMapMsg.sd13PCIUpLayer;
		break;
	case 'sd13PCIDownLayer':
		layer = mainMapMsg.sd13PCIDownLayer;
		break;
	case 'sd13RQIUpLayer':
		layer = mainMapMsg.sd13RQIUpLayer;
		break;
	case 'sd13RQIDownLayer':
		layer = mainMapMsg.sd13RQIDownLayer;
		break;
	case 'sd13SCIUpLayer':
		layer = mainMapMsg.sd13SCIUpLayer;
		break;
	case 'sd13SCIDownLayer':
		layer = mainMapMsg.sd13SCIDownLayer;
		break;
	case 'sd13TCIUpLayer':
		layer = mainMapMsg.sd13TCIUpLayer;
		break;
	case 'sd13TCIDownLayer':
		layer = mainMapMsg.sd13TCIDownLayer;
		break;
	case 'sd15MQIUpLayer':
		layer = mainMapMsg.sd15MQIUpLayer;
		break;
	case 'sd15MQIDownLayer':
		layer = mainMapMsg.sd15MQIDownLayer;
		break;
	case 'sd15PQIUpLayer':
		layer = mainMapMsg.sd15PQIUpLayer;
		break;
	case 'sd15PQIDownLayer':
		layer = mainMapMsg.sd15PQIDownLayer;
		break;
	case 'sd15PCIUpLayer':
		layer = mainMapMsg.sd15PCIUpLayer;
		break;
	case 'sd15PCIDownLayer':
		layer = mainMapMsg.sd15PCIDownLayer;
		break;
	case 'sd15RQIUpLayer':
		layer = mainMapMsg.sd15RQIUpLayer;
		break;
	case 'sd15RQIDownLayer':
		layer = mainMapMsg.sd15RQIDownLayer;
		break;
	case 'sd15SCIUpLayer':
		layer = mainMapMsg.sd15SCIUpLayer;
		break;
	case 'sd15SCIDownLayer':
		layer = mainMapMsg.sd15SCIDownLayer;
		break;
	case 'sd15TCIUpLayer':
		layer = mainMapMsg.sd15TCIUpLayer;
		break;
	case 'sd15TCIDownLayer':
		layer = mainMapMsg.sd15TCIDownLayer;
		break;
	}
	return layer;	
};
//矢量图层的渲染定义
var setVectorStyle = function(dataLayer){
	var style = null;
	switch (dataLayer.layerType){
	case "point":
		style =  new ol.style.Style({
			image: new ol.style.Icon({
				src: '../../gsj/images/gis/mark_blue.gif',
				scale: 0.5
			}),
		});
		break;
	case "line":
		style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#FFAA01',
				width: 4,
				lineDashOffset:50
			})
		});
		break;
	case  "polygon":
		style  = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#C20900',
				lineDash: [4],
				width: 3
			}),
			fill: new ol.style.Fill({
				color: 'rgba(0, 0, 255, 0.1)'
			})
		});
		break;
	}
	return style;
};
//移除指定专题图
var removeLayers = function(dataLayer){
	var arrLayer = map.getLayers();
	var layer = dataLayer.layerName;
	if(layer == 'suidao'){
		for(var i=0;i<dataLayer.layerArr.length;i++){
			var  name = dataLayer.layerArr[i];
			for(var j = 0;j<arrLayer.getLength();j++){
				var mapLayer = arrLayer.item(j);
				var layerName = mapLayer.get("layerName")||mapLayer.getProperties()["layerName"];
				if(layerName){
					if (layerName == name) {
						map.removeLayer(mapLayer);
						popup.setPosition(undefined);
						break;
					}
				}
			}
//			map.removeLayer(jkxtLayer);
//			map.removeLayer(zmxtLayer);
//			map.removeLayer(tfxtLayer);
		}
	}else if(layer == 'gd'){
		map.removeLayer(gdjhzd);
		removeLayer(dataLayer,arrLayer,layer);
	}else if(layer == 'shend'){
		map.removeLayer(sdjhzd);
		removeLayer(dataLayer,arrLayer,layer);
	}else if(layer == 'gs'){
		map.removeLayer(gsjhzd);
		removeLayer(dataLayer,arrLayer,layer);  
	}else if(layer == tempVector.layerName){
		map.removeLayer(markLayer);
		map.removeLayer(tempVector);
		removeLayer(dataLayer,arrLayer,layer);
	}else{
		removeLayer(dataLayer,arrLayer,layer);
	}
};
var removeLayer = function(dataLayer,arrLayer,layer){
	for(var i = 0;i<arrLayer.getLength();i++){
		var mapLayer = arrLayer.item(i);
		var layerName = mapLayer.get("layerName")||mapLayer.getProperties()["layerName"];
		if(layerName){
			if (layerName == dataLayer.layerName) {
				map.removeLayer(mapLayer);
				popup.setPosition(undefined);
				break;
			}
		}
	}
};
//移除地图的所有专题图
var removeAllLayer = function(judge){
	if(!judge){initMap();}
	var arrLayer = map.getLayers().getArray();
	var arr = [];
	for (var i=0;i<arrLayer.length;i++)
	{
		arr.push(arrLayer[i]);
	}
	for(var i = 0;i<arr.length;i++){
		var mapLayer = arr[i];
		var layerName =  mapLayer.get("layerName")||mapLayer.getProperties()["layerName"];
		if(layerName == null ||layerName==""||layerName != "gsjBase"){
			popup.setPosition(undefined); 
			map.removeLayer(mapLayer);
		}
	}
};

//点击地图查询
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
var popup = new ol.Overlay({
	element:container,
	autoPan: true,
	autoPanAnimation: {
		duration: 250
	}
});  
closer.onclick = function() {
	popup.setPosition(undefined);
	closer.blur();
	return false;
};
function mapClick(event){
	var coordinate = event.coordinate;
	var pixel = map.getEventPixel(event.originalEvent); 
	displayFeatureInfo(pixel);
	var featureInfo = map.forEachFeatureAtPixel(pixel,
			function (feature, layer) {
		return {feature:feature,layer:layer};
	});
	var roadInspectArr = ['gd11','gd13','gd15','sd11','sd13','sd15','xd11','xd15','yd11','yd15'];
	var roadProtectArr = ['gdfd','sdfd','xdfd','ydfd'];//生命防护道路专题图层数组
	var roadSafeArr = ['gdfd0','gdfd1','gdfd2','sdfd0','sdfd1','sdfd2',
	                   'xdfd0','xdfd1','xdfd2','ydfd0','ydfd1','ydfd2'];
	if (featureInfo!==undefined&&featureInfo!==null)
	{
		var layerName = "";
		if(featureInfo.layer !== undefined&&featureInfo.layer !==null){
			layerName  =  featureInfo.layer.get("layerName");
		}else{
			var  index = String(featureInfo.feature.id_).indexOf('.');
			layerName = String(featureInfo.feature.id_).substring(0,index);
		}
		if(roadInspectArr.indexOf(layerName) >= 0){
			map.removeOverlay(popup);
			var paramArr = [];
			var roadName = featureInfo.feature.get('name');
			paramArr.push(roadName);
			var roadCode = featureInfo.feature.get('code');
			paramArr.push(roadCode);
			var roadSxx =  featureInfo.feature.get('sxx')||" ";
			paramArr.push(roadSxx);
			searchInspectRoad(paramArr,layerName); 
		}else if(layerName == 'temp' || layerName == 'mark'||roadProtectArr.indexOf(layerName)>=0||layerName == ""){
			return;
		}else{
			var innerHTML = setPopHtml(layerName,featureInfo.feature.getProperties());
			content.innerHTML = innerHTML;
			popup.setPosition(coordinate);
			map.getView().setCenter(coordinate);
			map.addOverlay(popup);
			document.getElementById('popup-content').style.maxHeight='145px'; 
			document.getElementById('popup-content').className='';
		}  
	}else {

	}

};
var inspectRoadArr = [];
var tempVector = new ol.layer.Vector({
	layerName:'temp',
	source:new ol.source.Vector(),
	style:styleFunction
});
//查询表格查询路段
var searchInspectSection = function(paramArr,layerName){
	layerName = getLayerShorthand(layerName);
	map.removeOverlay(popup);
	var roadInspectArr = ['xd11','xd15','yd11','yd15'];
	var featureNSPara = queryConfig.queryGSDInspect.featureNS;
	var featurePrefixPara = queryConfig.queryGSDInspect.featurePrefix;
	var featureTypes = [layerName];
	var queryUrl = queryConfig.queryGSDInspect.url;
	var filter = ol.format.filter;
	var filterPara = filter.and(
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[0], paramArr[1]),
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[2], paramArr[2]),
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[3], paramArr[3]),
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[4], paramArr[4])
	); 
	if(roadInspectArr.indexOf(layerName) >= 0){
		filterPara = filter.and(
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[0], paramArr[0]),
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[1], paramArr[1]),
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[2], paramArr[3]),
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[3], paramArr[4])
		);
	};
	var featureRequest  = new ol.format.WFS().writeGetFeature({
		srsName:"EPSG:4326",
		featureNS:featureNSPara,
		featurePrefix:featurePrefixPara,
		featureTypes:featureTypes,
		filter:filterPara,
		outputFormat: "application/json"
	});
	fetch(queryUrl, {
		method: 'POST',
		body: new XMLSerializer().serializeToString(featureRequest)
	}).then(function(response) {
		return response.json();
	}).then(function(json) {
		var features = new ol.format.GeoJSON().readFeatures(json);
		if(features.length>0) {
			map.getView().fit(features[0].getGeometry().getExtent(), (map.getSize()));
			var center = getCoodinate(features[0].getGeometry());
			var innerHTML = setPopHtml(layerName,features[0].getProperties());
			content.innerHTML = innerHTML;
			popup.setPosition(center);
			map.addOverlay(popup);
			document.getElementById('popup-content').style.maxHeight='145px'; 
			document.getElementById('popup-content').className='';
		};
	});
};
//查询路检专题图层
var searchInspectRoad = function(paramArr,layerName){
	gsjmap.protectLayer = layerName+'MQI';
	gsjmap.roadCode = paramArr[1];
	gsjmap.roadSxx =  paramArr[2];
	var roadInspectArr = ['xd11','xd15','yd11','yd15'];
	var featureNSPara = queryConfig.queryGSDInspect.featureNS;
	var featurePrefixPara = queryConfig.queryGSDInspect.featurePrefix;
	var featureTypes = [layerName];
	var queryUrl = queryConfig.queryGSDInspect.url;
	var filter = ol.format.filter;
	var filterPara = filter.and(
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[0], paramArr[1]),
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[2], paramArr[2])
	); 
	if(roadInspectArr.indexOf(layerName) >= 0){
		filterPara = filter.and(
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[0], paramArr[0]),
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[1], paramArr[1])
		);
	};
	var featureRequest  = new ol.format.WFS().writeGetFeature({
		srsName:"EPSG:4326",
		featureNS:featureNSPara,
		featurePrefix:featurePrefixPara,
		featureTypes:featureTypes,
		filter:filterPara,
		outputFormat: "application/json"
	});
	fetch(queryUrl, {
		method: 'POST',
		body: new XMLSerializer().serializeToString(featureRequest)
	}).then(function(response) {
		return response.json();
	}).then(function(json) {
		var features = new ol.format.GeoJSON().readFeatures(json);

		if(features.length>0) {
			var resultArr = [];
			for(var i = 0;i<features.length;i++){
				resultArr.push(features[i].getProperties());
			}
			resultArr.sort(sortId);
			inspectRoadArr = features;
			Thematic_map.testTable(resultArr,getLayerName(layerName),layerName.substring(0,2));
			//randerInspectRoad(2448,2499);
		};
	});
};
//路检地图与数据表格的联动上下行切换
var switchInspectRoad = function(paramArr,layerName,roadName,callback){
	//alert(paramArr['upDown']);
	var layerCode = getLayerShorthand(layerName);
	var roadInspectArr = ['xd11','xd15','yd11','yd15'];
	var featureNSPara = queryConfig.queryGSDInspect.featureNS;
	var featurePrefixPara = queryConfig.queryGSDInspect.featurePrefix;
	var featureTypes = [layerCode];
	var queryUrl = queryConfig.queryGSDInspect.url;
	var filter = ol.format.filter;
	var filterPara = filter.and(
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[1],roadName),
			ol.format.filter.equalTo(queryConfig.queryGSDInspect.queryFieldName[2], paramArr['upDown'])
	); 
	if(roadInspectArr.indexOf(layerName) >= 0){
		filterPara = filter.and(
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[0], paramArr[1]),
				ol.format.filter.equalTo(queryConfig.queryXYDInspect.queryFieldName[1], paramArr[0])
		);
	};
	var featureRequest  = new ol.format.WFS().writeGetFeature({
		srsName:"EPSG:4326",
		featureNS:featureNSPara,
		featurePrefix:featurePrefixPara,
		featureTypes:featureTypes,
		filter:filterPara,
		outputFormat: "application/json"
	});
	fetch(queryUrl, {
		method: 'POST',
		body: new XMLSerializer().serializeToString(featureRequest)
	}).then(function(response) {
		return response.json();
	}).then(function(json) {
		// alert(0)
		var features = new ol.format.GeoJSON().readFeatures(json);
		if(features.length>0) {
			//alert(1);
			var resultArr = [];
			for(var i = 0;i<features.length;i++){
				resultArr.push(features[i].getProperties());
			}
			resultArr.sort(sortId);
			inspectRoadArr = features;

			callback.call(this,resultArr,layerName,layerCode.substring(0,2));
		};
	});
};
var markLayer = new ol.layer.Vector({
	layerName:'mark',
	source:new ol.source.Vector()
});
//路检专题图层根据起点止点桩号渲染图层
var randerInspectRoad = function(start,end,layer){
	tempVector.getSource().clear();
	markLayer.getSource().clear();
	map.removeLayer(tempVector);
	map.removeLayer(markLayer);
	for(var i = 0;i<inspectRoadArr.length;i++) {
		if(inspectRoadArr[i].get('mqipd') == '/' &&inspectRoadArr[i].get('pqipd')== '/'&&inspectRoadArr[i].get('rqipd') == '/'&&inspectRoadArr[i].get('scipd') == '/'&&
				inspectRoadArr[i].get('pcipd') == '/'&&inspectRoadArr[i].get('tcipd') == '/'){
			continue;
		}else{
			if(Number(inspectRoadArr[i].get('start')) == start){
				var coordiate = inspectRoadArr[i].getGeometry().getFirstCoordinate();
				var feature =  new ol.Feature({
					geometry:new ol.geom.Point(coordiate)
				});
				feature.setId('mark.1');
				feature.setStyle(new ol.style.Style({  
					image:new ol.style.Icon({ 
						src:'../../gsj/images/gis/qd.png',
						scale: 0.5,
						anchor: [0.5,0.8] 
					}) 
				}));
				markLayer.getSource().addFeature(feature);
			}
			if(Number(inspectRoadArr[i].get('end')) == end){
				var endCoordiate = inspectRoadArr[i].getGeometry().getLastCoordinate();
				var feature =  new ol.Feature({
					geometry:new ol.geom.Point(endCoordiate)
				});
				feature.setId('mark.2');
				feature.setStyle(new ol.style.Style({  
					image:new ol.style.Icon({ 
						src:'../../gsj/images/gis/zd.png',
						scale: 0.5,
						anchor: [0.5,0.8] 
					}) 
				}));
				markLayer.getSource().addFeature(feature);
			}
			if(Number(inspectRoadArr[i].get('start'))>=start&&Number(inspectRoadArr[i].get('end'))<=end){
				tempVector.getSource().addFeature(inspectRoadArr[i]);
				map.getView().fit(tempVector.getSource().getExtent(), (map.getSize()));
			}
		}
	}
	tempVector.layerName = getLayerShorthand(layer);
	//console.log(tempVector.layerName);
	map.addLayer(tempVector);
	map.addLayer(markLayer);
};

//根据检测指标渲染路况检测图层
var randerRoadByQuota = function(checkIndex){
	if(tempVector !=null){
		if(!checkIndex){
			tempVector.setStyle(styleFunction);
			return;
		}
		features  = tempVector.getSource().getFeatures();
		if(features.length>0){
			switch (checkIndex){
			case 'MQI':
				tempVector.setStyle(MQIStyleFunction);
				break;
			case 'PQI':
				tempVector.setStyle(PQIStyleFunction);
				break;
			case 'PCI':
				tempVector.setStyle(PCIStyleFunction);
				break;
			case 'RQI':
				tempVector.setStyle(RQIStyleFunction);
				break;
			case 'SCI':
				tempVector.setStyle(SCIStyleFunction);
				break;
			case 'TCI':
				tempVector.setStyle(TCIStyleFunction);
				break;
			}
		}
	}

};
//指标
var MQIStyleFunction = function(feature){
	var quato = feature.get('mqipd');
	var style = null;
	style = setInspectRoadStyle(quato);
	return style;
};
var PQIStyleFunction = function(feature){
	var quato = feature.get('pqipd');
	var style = null;
	style = setInspectRoadStyle(quato);
	return style;
};
//指标
var PCIStyleFunction  = function(feature){
	var quato = feature.get('pcipd');
	var style = null;
	style = setInspectRoadStyle(quato);
	return style;
};
//指标
var RQIStyleFunction  = function(feature){
	var quato = feature.get('rqipd');
	var style = null;
	style = setInspectRoadStyle(quato);
	return style;
};
//指标
var SCIStyleFunction = function(feature){
	var quato = feature.get('scipd');
	var style = null;
	style = setInspectRoadStyle(quato);
	return style;
};
var TCIStyleFunction = function(feature){
	var quato = feature.get('tcipd');
	var style = null;
	style = setInspectRoadStyle(quato);
	return style;
};
var setInspectRoadStyle = function(quato){
	var style = null;
	switch (quato){
	case '优':
		style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#008400',
				width: 8,
				lineCap:'butt'
			})
		});
		break;
	case '良':
		style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#0200F5',
				width: 8,
				lineCap:'butt'
			})
		});
		break;
	case '中':
		style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#FFFF02',
				width: 8,
				lineCap:'butt'
			})
		});
		break;
	case '次':
		style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#FF9201',
				width: 8,
				lineCap:'butt'
			})
		});
		break;
	case '差':
		style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#F8010C',
				width: 8,
				lineCap:'butt'
			})
		});
		break;
	}
	return [style];
};
//路检道路根据起始桩号排序
var sortId=function(a,b){
	return a.start-b.start; 
};
//根据图层设置弹窗信息
var setPopHtml = function(layer,values){
	var innerHTML = "";
	var roadArr = ["gd","ksdl","gs","xd","shend","cddl","qtdl",'xiangd'];
	var inspectArr = ["gd11","gd13","gd15","sd11","sd13","sd15","xd11","xd15","yd11","yd15"];
	// var roadProtectArr = ['GdProtect','ShendProtect','XdProtect','YdProtect'];
	//路检柱状图图层
	var roadEchartsArr = ['mqig11','mqig13','mqig15','mqis11','mqis13','mqis15',
	                      'pqig11','pqig13','pqig15','pqis11','pqis13','pqis15',
	                      'pcig11','pcig13','pcig15','pcis11','pcis13','pcis15',
	                      'rqig11','rqig13','rqig15','rqis11','rqis13','rqis15',
	                      'scig11','scig13','scig15','scis11','scis13','scis15',
	                      'tcig11','tcig13','tcig15','tcis11','tcis13','tcis15' ]; 
	var roadSafeArr = ['gdfd0','gdfd1','gdfd2','sdfd0','sdfd1','sdfd2',
	                   'xdfd0','xdfd1','xdfd2','ydfd0','ydfd1','ydfd2',
	                   ];
	if(roadArr.indexOf(layer)>=0){
		innerHTML = layerPopConfig.glMsg.setGlPopHtml(layer,values);
	}else if(layer == "guanlijigou" || layer == 'gonglugljg'){ 
		innerHTML = layerPopConfig.glglMsg.setguanlijigouPopHtml(layer,values);
	}else if(layer == "cxjiance"){
		innerHTML = layerPopConfig.cxjcMsg.setCxjcPopHtml(layer,values);
	}else if(layer == "glshoufei"){
		innerHTML = layerPopConfig.glsfMsg.setGlsfPopHtml(layer,values);
	}else if(layer == "luzheng"){
		innerHTML = layerPopConfig.lzxkMsg.setLzxkPopHtml(layer,values);
	}else if(layer == "zhuanghao"){
		innerHTML = layerPopConfig.zhuanghaoMsg.setZhuanghaoPopHtml(layer,values);
	}else if(layer == "jiaotong"){
		innerHTML = layerPopConfig.jiaotongMsg.setJtPopHtml(layer,values);
	}else if(layer == "ql"){
		innerHTML = layerPopConfig.qiaoliangMsg.setQiaoLiangPopHtml(layer,values);
	}else if(layer == "xingzheng"){
		innerHTML = layerPopConfig.xingzhengMsg.setXingZhengPopHtml(layer,values);
	}else if(layer == "sdx" || layer == "sd" ){
		innerHTML = layerPopConfig.suidaoMsg.setSuiDaoPopHtml(layer,values);
	}else if(roadSafeArr.indexOf(layer)>=0){
		var roleType = $('#roleType').val();
		// 是否可以编辑
		var editor = $('#editor').val();
		innerHTML = layerPopConfig.protectGlMsg.setProtectGlPopHtml(values, roleType, editor);
	}else if(layer == 'hdm'){
		innerHTML = layerPopConfig.hdmMsg.setHdmPopHtml(layer,values);
	}else if(roadEchartsArr.indexOf(layer)>=0){
		innerHTML = layerPopConfig.echarts_roadMsg.setEchartsRoadPopHtml(layer,values);
	}else if(inspectArr.indexOf(layer)>=0){
		innerHTML = layerPopConfig.inspect_SectionMsg.setInspectSectionPopHtml(layer,values);
	}
	return innerHTML;
};

//专题图层名称 
var getLayerName = function(layer){ 

	var layerName = "";
	switch (layer){
	case "gd":
		layerName = "国道";
		break;
	case "ksdl":
		layerName = "快速路";
		break;
	case "gs":
		layerName = "高速路";
		break;
	case "xd":
		layerName = "县道";
		break;
	case "shend": 
		layerName = "省道";
		break;
	case "cddl":
		layerName = "村道";
		break;
	case "qtdl":
		layerName = "其他道路";
		break;
	case "xiangd":
		layerName = "乡道";
		break;
	case "guanlijigou":
		layerName = "公路管理机构";
		break;
	case "gonglugljg":
		layerName = "公路管理机构";
		break;
	case "cxjiance":
		layerName = "超限检测站";
		break;
	case "glshoufei":
		layerName = "公路收费站";
		break;
	case "luzheng":
		layerName = "路政许可";
		break;
	case "zhuanghao":
		layerName = "主要桩号";
		break;
	case "xingzheng":
		layerName = "行政中心";
		break;
	case "jiaotong":
		layerName = "交通量调查";
		break;
	case "ql":
		layerName = "桥梁";
		break;
	case "gd11":
		layerName = "11年路检国道";
		break;
	case "gd13":
		layerName = "13年路检国道";
		break;
	case "gd15":
		layerName  = "15年路检国道";
		break;
	case "sd11":
		layerName  = "11年路检省道";
		break;
	case "sd13":
		layerName  = "13年路检省道";
		break;
	case "sd15":
		layerName  = "15年路检省道";
		break;
	case "xd11":
		layerName  = "11年路检县道";
		break;
	case "xd15":
		layerName  = "15年路检县道";
		break;
	case "yd11":
		layerName  = "11年路检乡道";
		break;
	case "yd15":
		layerName  = "15年路检乡道";
		break;
	case "sdx":
		layerName = "隧道";
		break;
	case "sd":
		layerName = "隧道";
		break;
	case 'GdProtect':
		layerName = '国道生命防护';
		break;
	case 'ShendProtect':
		layerName = '省道生命防护';
		break;
	case  'XdProtect':
		layerName = '县道生命防护';
		break;
	case 'YdProtect':
		layerName = '乡道生命防护';
		break;
	case 'hdm':
		layerName = '隧道横道面';
		break; 
	case "g11":
		layerName = "11年路检柱状国道";
		break;
	case "g13":
		layerName = "13年路检柱状国道";
		break;
	case "g15":
		layerName  = "15年路检柱状国道";
		break;
	case "s11":
		layerName  = "11年路检柱状省道";
		break;
	case "s13":
		layerName  = "13年路检柱状省道";
		break;
	case "s15":
		layerName  = "15年路检柱状省道";
		break;
	}

	return layerName;
};

var  getLayerShorthand = function(layer){
	var layerName = "";
	switch (layer){
	case "11年路检国道":
		layerName = "gd11";
		break;
	case "13年路检国道":
		layerName = "gd13";
		break;
	case "15年路检国道":
		layerName = "gd15";
		break;
	case "11年路检省道":
		layerName  = "sd11";
		break;
	case "13年路检省道":
		layerName  = "sd13";
		break;
	case "15年路检省道":
		layerName  = "sd15";
		break;
	case "11年路检县道":
		layerName  = "xd11";
		break;
	case "15年路检县道":
		layerName  = "xd15";
		break;
	case "11年路检乡道":
		layerName  = "yd11";
		break;
	case "15年路检乡道":
		layerName  = "yd15";
		break;

	}
	return layerName;
};

/** 
 * 鼠标移动的事件  
 */  
var featureOverlay = new ol.layer.Vector({
	source: new ol.source.Vector(),
	map: map,
	style:styleFunction
});

var highlight;
var displayFeatureInfo = function(pixel,coordinate) {
	var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
		return feature;
	});
	featureOverlay.setVisible(true);
	var roadInspectArr = ['gd11','gd13','gd15','sd11','sd13','sd15','xd11','xd15','yd11','yd15'];
	var roadProtectArr = ['gdfd','sdfd','xdfd','ydfd'];
	var roadSafeArr = ['gdfd0','gdfd1','gdfd2','sdfd0','sdfd1','sdfd2',
	                   'xdfd0','xdfd1','xdfd2','ydfd0','ydfd1','ydfd2',
	                   ];

	if(feature){
		var layerName = String(feature.id_).substring(0,String(feature.id_).indexOf('.'));
		if(roadInspectArr.indexOf(layerName)>=0||roadProtectArr.indexOf(layerName)>=0){
			featureOverlay.setVisible(false);
		}else if(roadSafeArr.indexOf(layerName)>=0){
			var innerHTML = setPopHtml(layerName,feature.getProperties());
			content.innerHTML = innerHTML;
			popup.setPosition(coordinate);
			map.addOverlay(popup);
			document.getElementById('popup-content').style.maxHeight='145px'; 
			document.getElementById('popup-content').className='';
		}
	}
	if (feature !== highlight) {
		if (highlight) {
			featureOverlay.getSource().removeFeature(highlight);
			document.querySelector("#map").style.cursor = "default";
		}
		if(feature) {
			featureOverlay.getSource().addFeature(feature);
			document.querySelector("#map").style.cursor = "pointer";
		}
		highlight = feature;   
	}
};
map.on('pointermove', function(evt) {  
	if (evt.dragging) {   //如果是拖动地图造成的鼠标移动，则不作处理  
		return;  
	}  
	var pixel = map.getEventPixel(evt.originalEvent);
	displayFeatureInfo(pixel,evt.coordinate);
});  

//地图导出jpg格式图片
var exportPNGElement = document.getElementById('export-jpg'); //导出图片功能项
if ('download' in exportPNGElement) {
	exportPNGElement.addEventListener('click', function (e) {
		map.once('postcompose', function (event) {
			var canvas = event.context.canvas; //地图渲染容器
			// console.log(canvas);
			exportPNGElement.href = canvas.toDataURL('image/jpg'); //导出图片
			//console.log(exportPNGElement.href);
		});
		map.renderSync();
	}, false);
} else {
	alert("请使用谷歌浏览器导出地图图片");
}

//查询所有
var searchAll = function(searchType,callback){
	var featureNSPara = queryConfig.queryRoad.featureNS;
	var featurePrefixPara = queryConfig.queryRoad.featurePrefix;
	var featureTypes = queryConfig.queryRoad.featureTypes;
	var queryUrl = queryConfig.queryRoad.url;
	if(searchType == 'qiaoliang'){
		featureNSPara = queryConfig.queryBridge.featureNS;
		featurePrefixPara = queryConfig.queryBridge.featurePrefix;
		featureTypes = queryConfig.queryBridge.featureTypes;
		queryUrl = queryConfig.queryBridge.url;
	}else if(searchType == 'suidao'){
		featureNSPara = queryConfig.queryTunnel.featureNS;
		featurePrefixPara = queryConfig.queryTunnel.featurePrefix;
		featureTypes = queryConfig.queryTunnel.featureTypes;
		queryUrl = queryConfig.queryTunnel.url;
	}
	var source = new ol.source.Vector();
	var vector = new ol.layer.Vector({
		source:source,
		style:styleFunction
	});
	var featureRequest  = new ol.format.WFS().writeGetFeature({
		srsName:"EPSG:4326",
		featureNS:featureNSPara,
		featurePrefix:featurePrefixPara,
		featureTypes:featureTypes,
		outputFormat: "application/json",
	});
	fetch(queryUrl, {
		method: 'POST',
		body: new XMLSerializer().serializeToString(featureRequest)
	}).then(function(response) {
		return response.json();
	}).then(function(json) {
		var features = new ol.format.GeoJSON().readFeatures(json);
		if(features.length>0) {
			source.addFeatures(features);
			map.getView().fit(source.getExtent(), (map.getSize()));
		}
		callback.call(this,searhResult(vector));
	});

};
//根据条件搜索
var searchRoadByText = function(searchType,searchValue,callback){ 
	var searchText = "*"+searchValue+"*";
	var filter = ol.format.filter;
	var filterPara = filter.or(
			ol.format.filter.like(queryConfig.queryRoad.queryFieldName[0], searchText),
			ol.format.filter.like(queryConfig.queryRoad.queryFieldName[1], searchText)
	) ;
	var featureNSPara = queryConfig.queryRoad.featureNS;
	var featurePrefixPara = queryConfig.queryRoad.featurePrefix;
	var featureTypes = queryConfig.queryRoad.featureTypes;
	var queryUrl = queryConfig.queryRoad.url;
	if(searchType == 'qiaoliang'){
		featureNSPara = queryConfig.queryBridge.featureNS;
		featurePrefixPara = queryConfig.queryBridge.featurePrefix;
		featureTypes = queryConfig.queryBridge.featureTypes;
		queryUrl = queryConfig.queryBridge.url;
		filterPara = filter.or(
				ol.format.filter.like(queryConfig.queryBridge.queryFieldName[0], searchText),
				ol.format.filter.like(queryConfig.queryBridge.queryFieldName[1], searchText)
		); 
	}else if(searchType == 'suidao'){
		featureNSPara = queryConfig.queryTunnel.featureNS;
		featurePrefixPara = queryConfig.queryTunnel.featurePrefix;
		featureTypes = queryConfig.queryTunnel.featureTypes;
		queryUrl = queryConfig.queryTunnel.url;
		filterPara = filter.or(
				ol.format.filter.like(queryConfig.queryTunnel.queryFieldName[0], searchText),
				ol.format.filter.like(queryConfig.queryTunnel.queryFieldName[1], searchText)
		);
	}
	var source = new ol.source.Vector();
	var vector = new ol.layer.Vector({
		source:source,
		style:styleFunction
	});
	var featureRequest  = new ol.format.WFS().writeGetFeature({
		srsName:"EPSG:4326",
		featureNS:featureNSPara,
		featurePrefix:featurePrefixPara,
		featureTypes:featureTypes,
		outputFormat: "application/json",
		filter:filterPara   
	});
	fetch(queryUrl, {
		method: 'POST',
		body: new XMLSerializer().serializeToString(featureRequest)
	}).then(function(response) {
		return response.json();
	}).then(function(json) {
		var features = new ol.format.GeoJSON().readFeatures(json);
		if(features.length>0) {
			source.addFeatures(features);
			map.getView().fit(source.getExtent(), (map.getSize()));
		}
		callback.call(this,searhResult(vector));
	});

};
//查询结果
var searhResult = function(vector){
	var resultArr = [];
	var  featureArr = vector.getSource().getFeatures();
	if(featureArr == null || featureArr.length<=0){
		resultArr = null;
	}else{
		for(var i =0;i<featureArr.length;i++){
			resultArr.push(featureArr[i].getProperties());		     
		}
	}
	return resultArr;
};


//道路编号查询道路、桥梁、隧道
var queryByCode = function(searchType,code,roadStartName){
	if(code){
		var filter = ol.format.filter;
		var filterPara = filter.and(
				ol.format.filter.equalTo(queryConfig.queryRoad.queryFieldName[1], code),
				ol.format.filter.equalTo(queryConfig.queryRoad.queryFieldName[2], roadStartName)
		) ;
		var featureNSPara = queryConfig.queryRoad.featureNS;
		var featurePrefixPara = queryConfig.queryRoad.featurePrefix;
		var featureTypes = queryConfig.queryRoad.featureTypes;
		var queryUrl = queryConfig.queryRoad.url;
		if(searchType == "qiaoliang"){
			featureNSPara = queryConfig.queryBridge.featureNS;
			featurePrefixPara = queryConfig.queryBridge.featurePrefix;
			featureTypes = queryConfig.queryBridge.featureTypes;
			queryUrl = queryConfig.queryBridge.url;
			filterPara = ol.format.filter.equalTo(queryConfig.queryBridge.queryFieldName[0], code);
		}else if(searchType == "suidao"){
			featureNSPara = queryConfig.queryTunnel.featureNS;
			featurePrefixPara = queryConfig.queryTunnel.featurePrefix;
			featureTypes = queryConfig.queryTunnel.featureTypes;
			queryUrl = queryConfig.queryTunnel.url;
			filterPara = ol.format.filter.equalTo(queryConfig.queryTunnel.queryFieldName[0], code);
		}
		var source = new ol.source.Vector();
		var vector = new ol.layer.Vector({
			source: source,
			style:styleFunction
		});
		var featureRequest = new ol.format.WFS().writeGetFeature({
			srsName:"EPSG:4326",
			featureNS:featureNSPara,
			featurePrefix:featurePrefixPara,
			featureTypes:featureTypes,
			outputFormat: 'application/json',
			filter:filterPara
		});
		// post请求，并根据请求的数据进行加载
		fetch(queryUrl, {
			method: 'POST',
			body: new XMLSerializer().serializeToString(featureRequest)
		}).then(function(response) {
			return response.json();
		}).then(function(json) {
			var features = new ol.format.GeoJSON().readFeatures(json);
			if(features.length>0) {
				source.addFeatures(features);
			}
			queryRoadShow(code,vector);
		});
	}	
};
//根据查询道路结果显示定位
var  queryRoadShow = function (seachValue,vector){
	var  featureArr = vector.getSource().getFeatures();
	if(featureArr == null || featureArr.length<=0){

	}else{
		removeAllLayer();
		popAndLocation(vector);   //弹窗显示并定位显示点击的要素
	}
};
//定位并弹窗显示
var popAndLocation = function (vector){
	var source  = vector.getSource();
	var features = source.getFeatures();
	var  index = String(features[0].id_).indexOf('.');		
	var layerName = String(features[0].id_).substring(0,index);
	var innerHTML = setPopHtml(layerName,features[0].getProperties());
	var  geometry = features[0].getGeometry();
	var coodiate = getCoodinate(geometry);
	content.innerHTML = innerHTML;
	popup.setPosition(coodiate);
	map.getView().setCenter(coodiate);
	map.getView().fit(source.getExtent(),(map.getSize()));
	map.addLayer(vector);
	map.addOverlay(popup); 
	document.getElementById('popup-content').style.maxHeight='145px'; 
	document.getElementById('popup-content').className='';
};

//获取要素点坐标
var getCoodinate = function(geometry){
	var center = [];
	if(geometry instanceof ol.geom.Point){
		center = geometry.getCoordinates();
		var x = center[0]+0.000009;
		var y = center[1]+0.000009;
		center = [x,y];
	}else if(geometry instanceof ol.geom.MultiPoint){
		center = geometry.getFirstCoordinate();
		var x = center[0]+0.000009;
		var y = center[1]+0.000009;
		center = [x,y];
	}else if(geometry instanceof ol.geom.LineString){
		var coodinateArr = geometry.getCoordinates();
		if(coodinateArr.length%2 == 0){
			center =  coodinateArr[coodinateArr.length/2];
		}else{
			center =  coodinateArr[(coodinateArr.length+1)/2-1];
		}
	}else if(geometry instanceof  ol.geom.MultiLineString){
		//var lineNumber = geometry.getLineStrings().length;
		var coodinateArr = geometry.getCoordinates();
		if(coodinateArr.length%2 == 0){
			var coodinates =  coodinateArr[coodinateArr.length/2];
			var length = Number(coodinates.length);
			if(length%2 == 0){
				center =  coodinates[coodinates.length/2];
			}else{
				center =  coodinates[(coodinates.length+1)/2-1];
			}
		}else{
			var coodinates =  coodinateArr[(coodinateArr.length+1)/2-1];
			var length = Number(coodinates.length);
			if(length%2 == 0){
				center =  coodinates[length/2];
			}else{
				center =  coodinates[(length+1)/2-1];
			}
		}
	}else if(geometry instanceof   ol.geom.Polygon){
		center = geometry.getInteriorPoint().getCoordinates();
	}else if(geometry instanceof ol.geomMultiPolygon){
		center = geometry.getInteriorPoints().getFirstCoordinate();
	}
	return center;
};
//弹窗 查看详情
function ViewDetails(e){
	e.parentNode.style.display='none';
	document.getElementById('popup-content').className='on';
	document.getElementById('popup-content').style.maxHeight=''; 
	document.getElementById('popup-content').style.height='auto'; 
};
var getLayerFeature = function(layerName){
	var arrLayer = map.getLayers().getArray();
	var arr = [];
	var features = [];
	for (var i=0;i<arrLayer.length;i++)
	{
		arr.push(arrLayer[i]);
	}
	for(var i = 0;i<arr.length;i++){
		var mapLayer = arr[i];
		var layer =  mapLayer.get("layerName")||mapLayer.getProperties()["layerName"];
		if(layer == layerName){
			console.log(mapLayer.getSource().getFeatures());
			//features = mapLayer.getSource().getFeatures();
		}
	}
	return features;
};
//根据生命防护背景图层和后台数据和工程状态动态生成加载防护专题图层
var addProtectLayer = function(dataLayer,state,protectArr){
	if(dataLayer!=null){
		if(state == null){
			Loding("show");
			var vectorSource = new ol.source.Vector(
					{
						url:function(extent,resolution,projection){
							return dataLayer.layerUrl+extent.join(',');
						},
						format:new ol.format.GeoJSON(),
						strategy:ol.loadingstrategy.bbox 
					});
			var  vectorLayer = new ol.layer.Vector({
				layerName:dataLayer.layerName,
				source:vectorSource,
				style:dataLayer.style
			});    
			map.addLayer(vectorLayer);	
			Loding("hide");
		}else{
			var name = dataLayer.layerName;
			var arrLayer = map.getLayers();
			var vectorLayer = null;
			for(var j = 0;j<arrLayer.getLength();j++){
				var mapLayer = arrLayer.item(j);
				var layerName = mapLayer.get("layerName")||mapLayer.getProperties()["layerName"];
				if(layerName){
					if (layerName == name) {
						vectorLayer = mapLayer;
						break;
					}
				}
			}
			if(vectorLayer == null){
				var vectorSource = new ol.source.Vector(
						{
							url:function(extent,resolution,projection){
								return dataLayer.layerUrl+extent.join(',');
							},
							format:new ol.format.GeoJSON(),
							strategy:ol.loadingstrategy.bbox 
						});
				vectorLayer = new ol.layer.Vector({
					layerName:dataLayer.layerName,
					source:vectorSource,
				});
				map.addLayer(vectorLayer);
			}
			Loding("show");
			// 因为是异步加载，所以要采用事件监听的方式来判定是否加载完成
			var listenerKey = vectorLayer.getSource().on('change', function(){
				if (vectorLayer.getSource().getState() === 'ready') {    // 判定是否加载完成
					features  = vectorLayer.getSource().getFeatures();
					createLifeProtect(state,features,dataLayer.layerName,protectArr);
					vectorLayer.getSource().un(listenerKey); // 注销监听器
				}
			});
		}
	}
};
//根据状态构建生命防护专题图层并加载在地图上
var createLifeProtect = function(state,featuresArr,layerName,protectArr){
	var layerNameFirst = layerName.substring(0,1).toUpperCase( );
	var untreatedArr = [];
	var processedArr = [];
	var featureArr = [];
	var evaluatedArr = [];
	if(state == '0'){
		for(var i = 0;i<protectArr.length;i++){
			if(protectArr[i].delStatus == '0'&&protectArr[i].roadCode.substring(0,1) == layerNameFirst){
				untreatedArr.push(protectArr[i]);
			}
		}
		for(var i=0;i<untreatedArr.length;i++){
			for(var j=0;j<featuresArr.length;j++){
				var roadcode = featuresArr[j].get('roadcode');
				if(layerNameFirst == 'X' || layerNameFirst == 'Y'){
					roadcode = roadcode.substring(0,4);
				}
				var startzh = Number(featuresArr[j].get('startzh'));
				var endzh = Number(featuresArr[j].get('endzh'));
				var startPegNum = Number(untreatedArr[i].startPegNum);
				var endPegNum = Number(untreatedArr[i].endPegNum);
				var middlePegNum = (startPegNum+endPegNum)/2;
				if(roadcode == untreatedArr[i].roadCode && startzh<middlePegNum&&middlePegNum<=endzh){
					var coordiate = getCoodinate(featuresArr[j].getGeometry()); 
					var feature =  new ol.Feature({
						geometry:new ol.geom.Point(coordiate)
					});
					feature.setStyle(new ol.style.Style({  
						image:new ol.style.Icon({ 
							src:'../../gsj/images/gis/mark_red.gif',
							scale: 0.4,
							anchor: [0.5, 1] 

						}) 
					}));
					feature.setId(layerName+state+'.'+j);
					feature.set('id',untreatedArr[i].id,true);
					feature.set('roadName',featuresArr[j].get('roadname'),true);
					feature.set('roadCode',untreatedArr[i].roadCode,true);
					feature.set('startPegNum',Number(untreatedArr[i].startPegNum).toFixed(3),true);
					feature.set('endPegNum',Number(untreatedArr[i].endPegNum).toFixed(3),true);
					feature.set('year',untreatedArr[i].investigationTime,true);
					feature.set('delStatus',untreatedArr[i].delStatus,true);
					feature.set('currentStatus',untreatedArr[i].currentStatus,true);
					featureArr.push(feature);
					//   protectVector.getSource().addFeature(feature);
				}
			}
		}
	}else if(state == '1'){
		for(var i = 0;i< protectArr.length;i++){
			if(protectArr[i].delStatus == '1'&&protectArr[i].roadCode.substring(0,1) == layerNameFirst){
				processedArr.push(protectArr[i]);
			}
		}
		for(var i=0;i<processedArr.length;i++){
			for(var j=0;j<featuresArr.length;j++){
				var roadcode = featuresArr[j].get('roadcode');
				if(layerNameFirst == 'X' || layerNameFirst == 'Y'){
					roadcode = roadcode.substring(0,4);
				}
				var startzh = Number(featuresArr[j].get('startzh'));
				var endzh = Number(featuresArr[j].get('endzh'));
				var startPegNum = Number(processedArr[i].startPegNum);
				var endPegNum = Number(processedArr[i].endPegNum);
				var middlePegNum = (startPegNum+endPegNum)/2;
				if(roadcode == processedArr[i].roadCode &&startzh<middlePegNum&&middlePegNum<=endzh){
					var coordiate = getCoodinate(features[j].getGeometry()); 
					var feature =  new ol.Feature({
						geometry:new ol.geom.Point(coordiate)
					});
					feature.setStyle(new ol.style.Style({  
						image:new ol.style.Icon({ 
							src:'../../gsj/images/gis/mark_blue.gif',
							color:'#2f4554',
							scale: 0.4,
							anchor: [0.5, 1] 
						}) 
					}));
					feature.setId(layerName+state+'.'+j);
					feature.set('id',processedArr[i].id,true);
					feature.set('roadName',featuresArr[j].get('roadname'),true);
					feature.set('roadCode',processedArr[i].roadCode,true);
					feature.set('startPegNum',Number(processedArr[i].startPegNum).toFixed(3),true);
					feature.set('endPegNum',Number(processedArr[i].endPegNum).toFixed(3),true);
					feature.set('year',processedArr[i].investigationTime,true);
					feature.set('delStatus',processedArr[i].delStatus,true);
					feature.set('currentStatus',processedArr[i].currentStatus,true);
					featureArr.push(feature);
				}
			}
		}
	}else  if(state == '2'){
		for(var i = 0;i< protectArr.length;i++){
			if(protectArr[i].delStatus == '2'&&protectArr[i].currentStatus == '5'&&protectArr[i].roadCode.substring(0,1) == layerNameFirst){
				evaluatedArr.push(protectArr[i]);
			}
		}
		for(var i=0;i<evaluatedArr.length;i++){
			for(var j=0;j<featuresArr.length;j++){
				var roadcode = featuresArr[j].get('roadcode');
				if(layerNameFirst == 'X' || layerNameFirst == 'Y'){
					roadcode = roadcode.substring(0,4);
				}
				var startzh = Number(featuresArr[j].get('startzh'));
				var endzh = Number(featuresArr[j].get('endzh'));
				var startPegNum = Number(evaluatedArr[i].startPegNum);
				var endPegNum = Number(evaluatedArr[i].endPegNum);
				var middlePegNum = (startPegNum+endPegNum)/2;
				if(roadcode == evaluatedArr[i].roadCode &&startzh<middlePegNum&&middlePegNum<=endzh){
					var coordiate = getCoodinate(features[j].getGeometry()); 
					var feature =  new ol.Feature({
						geometry:new ol.geom.Point(coordiate)
					});
					feature.setStyle(new ol.style.Style({  
						image:new ol.style.Icon({ 
							src:'../../gsj/images/gis/mark_green.gif',
							scale: 0.4,
							anchor: [0.5, 1] 
						}) 
					}));
					feature.setId(layerName+state+'.'+j);
					feature.set('id',evaluatedArr[i].id,true);
					feature.set('roadName',featuresArr[j].get('roadname'),true);
					feature.set('roadCode',evaluatedArr[i].roadCode,true);
					feature.set('startPegNum',Number(evaluatedArr[i].startPegNum).toFixed(3),true);
					feature.set('endPegNum',Number(evaluatedArr[i].endPegNum).toFixed(3),true);
					feature.set('year',evaluatedArr[i].investigationTime,true);
					feature.set('delStatus',evaluatedArr[i].delStatus,true);
					feature.set('currentStatus',evaluatedArr[i].delStatus,true);
					featureArr.push(feature);
				}
			}
		}

	}
	if(layerName == 'gdfd'&&state == '0'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'gdfd0',
			source:vetorSource
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'gdfd'&&state == '1'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'gdfd1',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'gdfd'&&state == '2'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'gdfd2',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'sdfd'&&state == '0'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'sdfd0',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'sdfd'&&state == '1'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'sdfd1',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'sdfd'&&state == '2'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'sdfd2',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'xdfd'&&state == '0'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'xdfd0',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'xdfd'&&state == '1'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'xdfd1',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'xdfd'&&state == '2'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'xdfd2',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'ydfd'&&state == '0'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'ydfd0',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'ydfd'&&state == '1'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'ydfd1',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}else if(layerName == 'ydfd'&&state == '2'){
		var vetorSource = new ol.source.Vector({
			features:featureArr
		});
		var vectorLayer = new ol.layer.Vector({
			layerName:'ydfd2',
			source:vetorSource,
		});
		map.addLayer(vectorLayer);
	}
	Loding("hide");	
};